package northburns.gamedude.foobar;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class FooBar {
	public static class SomeRequest {
		public String text;
	}

	public static class SomeResponse {
		public String text;
	}

	public static void main(String[] args) throws Exception {
		Server server = new Server();
		server.start();
		server.bind(54555, 54777);

		System.out.println("Server started");

		server.addListener(new Listener() {
			public void received(Connection connection, Object object) {
				if (object instanceof SomeRequest) {
					SomeRequest request = (SomeRequest) object;
					System.out.println(request.text);

					SomeResponse response = new SomeResponse();
					response.text = "Thanks!";
					connection.sendTCP(response);
				}
			}
		});

		Client client = new Client();
		client.start();
		client.connect(5000, "127.0.0.1", 54555, 54777);

		client.addListener(new Listener() {
			public void received(Connection connection, Object object) {
				if (object instanceof SomeResponse) {
					SomeResponse response = (SomeResponse) object;
					System.out.println(response.text);
				}
			}
		});

		Kryo kryo = server.getKryo();
		kryo.register(SomeRequest.class);
		kryo.register(SomeResponse.class);
		kryo = client.getKryo();
		kryo.register(SomeRequest.class);
		kryo.register(SomeResponse.class);

		SomeRequest request = new SomeRequest();
		request.text = "Here is the request!";
		client.sendTCP(request);

	}

}
